﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Configuration;
using System.Web.Security;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using System.Collections;

public partial class newgoods : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["name"] == null)
        {
            Response.Redirect("~/error.aspx?");
        }
        if (!IsPostBack)
        {
            this.bind();
        }
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        if (TextBox1.Text.Trim() == "" || TextBox2.Text.Trim() == "" || TextBox3.Text.Trim() == "" || TextBox4.Text.Trim() == "" || TextBox7.Text.Trim() == "")
        {
            Response.Write("<script>alert('请填写必要的完整信息!')</script>");
        }
        else if (TextBox1.Text.Trim().Length > 90)
        {
            Response.Write("<script>alert('货品号过长!')</script>");
        }
        else
        {
            SqlConnection myConn = GetConnection();
            myConn.Open();
            string sqlcon = "select CID from Categories where CName='" + DropDownList1.SelectedValue + "'";
            SqlCommand mycon = new SqlCommand(sqlcon, myConn);
            mycon.ExecuteNonQuery();
            SqlDataReader read = mycon.ExecuteReader();
            read.Read();
            string CID = read["CID"].ToString();
            read.Close();

            string sqlStr = "insert into [Goods](GCHName,GTYPEName,GCategory,GUnit,GProvider1,GProvider2,GDescribe,GXinhao,GBrand) values('" + TextBox1.Text + "','" + DropDownList1.SelectedValue + "','" + CID + "','" + TextBox3.Text + "','" + TextBox5.Text + "','" + TextBox6.Text + "','" + TextBox7.Text + "','" + TextBox2.Text + "','" + TextBox4.Text + "')";
            SqlCommand myCmd = new SqlCommand(sqlStr, myConn);
            myCmd.ExecuteNonQuery();

            string sqlQ = "select * from Goods where GCHName='" + TextBox1.Text + "' and GXinhao='" + TextBox2.Text + "' and GBrand='" + TextBox4.Text + "'";
            SqlCommand myQ = new SqlCommand(sqlQ, myConn);
            myQ.ExecuteNonQuery();
            SqlDataReader readQ = myQ.ExecuteReader();
            readQ.Read();
            string GID = readQ["GID"].ToString();
            readQ.Close();

            string sqlSql = "insert into ProjectsQuantity(PID,PGID,PGName,PQuantity) values('6','"+ Convert.ToInt32(GID) +"','" + TextBox1.Text + "','0')";
            SqlCommand mySql = new SqlCommand(sqlSql, myConn);
            mySql.ExecuteNonQuery();
            myConn.Close();
            Response.Redirect("~/success.aspx?");
        }
    }
    protected void Button2_Click(object sender, EventArgs e)
    {
        Response.Redirect("~/goodsinfor.aspx?");
    }

    public SqlConnection GetConnection()
    {
        string myStr = ConfigurationManager.AppSettings["ConnectionString"].ToString();
        SqlConnection myConn = new SqlConnection(myStr);
        return myConn;
    }

    public void bind()
    {
        SqlConnection myConn = GetConnection();
        myConn.Open();


        string sql = "select CName from Categories";
        SqlDataAdapter sqlda = new SqlDataAdapter(sql, myConn);
        DataSet sqlds = new DataSet();
        sqlda.Fill(sqlds);
        DropDownList1.DataSource = sqlds;
        DropDownList1.DataTextField = "CName";
        DropDownList1.DataBind();
        sqlda.Dispose();
        sqlds.Dispose();

        myConn.Close();
    }

}